import store from '@neu/biz-core/store'

export default {
  inserted(el, binding, vnode) {
    const { value } = binding
    const roles = store.getters && store.getters.roles
    const isAdmin = roles.some(r => r === 'admin')
    const buttons = store.getters && store.getters.allowed_buttons

    if (value && value instanceof Array && value.length > 0) {
      const permissionButtons = value

      const hasPermission = isAdmin || buttons.some(btn => {
        return permissionButtons.includes(btn)
      })

      if (!hasPermission) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    } else {
      throw new Error(`need buttons! Like v-permission="['admin','editor']"`)
    }
  }
}
